layui.define(['form', 'table'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form;

  var deviceSn =$('input[name="deviceSn"]').val();
  var deviceTypeId =$('input[name="deviceTypeId"]').val();

    $('#userId').select2({
        placeholder: "请选择设备归属人",
        width: '250px',
        theme: 'bootstrap',
        allowClear : true,
        ajax: {
            url: layui.setter.webRoot + '/agent/select.data',
            method: 'post',
            delay: 250,
            processResults: function (result) {
                if (result.code == 200) {
                    return {
                        pagination: {more: result.body.more},
                        results: result.body.data || []
                    };
                } else {
                    return {results: []};
                }
            }
        }
    });

    form.on('submit(LAY-update)', function(data){
        var field = data.field;
        layer.confirm('确定要修改吗？', function(index){
            $.ajax({
                type: "post",
                url: layui.setter.webRoot + '/device/update.do',
                data:field,
                dataType:"json",
                success:function(res){
                    if(res.code == 200){
                        location.reload();
                    }else{
                        layer.msg(res.msg,{icon: 5});
                    }
                }
            });
            layer.close(index);
        });
    });

    table.render({
        elem: '#LAY-table'
        ,url: layui.setter.webRoot + '/device/type/func/list.data'
        ,where:{typeId:deviceTypeId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'code',title: '功能Code',templet:function(record){
                return record.codeName + '('+record.code+')';
            }}
            ,{field: 'businessType', title: '业务Code',templet:function(record){
                if(record.businessType) {
                    return record.businessTypeName + '(' + record.businessType + ')';
                }else{
                    return '---';
                }
            }}
            ,{field: 'slotTotal',title: '卡槽数'}
            ,{field: 'note',title: '备注'}
            ,{field: 'createDate', title: '创建时间'}
            ,{title: '操作', width: 90, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
        ]]
        ,page: false
        ,height: 'full-220'
        ,text: '对不起，加载出现异常！'
    });

    var active = {
        deviceInfo: function (obj) {
            parent.layer.open({
                type: 2
                , title: '查看设备信息'
                , content: layui.setter.webRoot + '/device/func/info.html?deviceSn='+deviceSn+'&funCode='+obj.data.code
                , maxmin: true
                , area: ['80%', '80%']
                , btn: ['取消']
            });
        }
    }

    $('.layui-btn.LAY-btn-fun').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    table.on('tool(LAY-table)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });
  
  exports('device_info', {})
});